home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / Amortize1.x / Source / Amort.m < prev    next >
Text File  |  1993-10-11  |  9KB  |  361 lines

  1.  
  2. // Generated by Interface Builder
  3. // Modified by Eric Tremblay May 16, 1992
  4. // Revised on January 24, 1993
  5.  
  6. #import "Amort.h"
  7. #include <math.h>
  8.  
  9. // Gobal Variables
  10.  
  11.     const char *fileName; // The fileName for the SaveFile method
  12.     int saveOK;           // Used by the SavePanel in the getName method
  13.     char buffer[800];     // Text buffer
  14.  
  15. @implementation Amort
  16.  
  17. // AWAKEFROMNIB
  18. // This is one of the last methods executed after the nib file is loaded.
  19. // This is the BEST way I know how to initialize an object and jump into
  20. // a method after the nib file was loaded, because you are all the methods
  21. // and variables needed can be used. (Unlike init which is not compeletly
  22. // ready when that message is sent
  23. - awakeFromNib
  24. {
  25.       long time(), tmpTime;
  26.     struct tm *tm, *localtime();
  27.     
  28.     tmpTime = time(0);
  29.     tm = localtime( &tmpTime );
  30.     sysMonth = (tm->tm_mon+1);
  31.     sysYear = (1900+tm->tm_year);
  32.         mbeg = sysMonth; // This will set the default for the month of January
  33.     [NXApp activateSelf:YES]; //activate the application 
  34.     
  35.     return self; //normal return
  36. }
  37.  
  38.  
  39.  
  40. // CALC
  41. - Calc:sender
  42. // This method generates an amortization table and prints the result
  43. // in the various fields and in the ScrollView (Only outputs to the Screen)
  44. {
  45.  
  46. // Declare the variables
  47.         
  48.     char pmtString[20]; // (payment) pmt float value in a string
  49.     char icString[20];  // (Interest chaged) ic float value in a string
  50.     char fvString[20];  // (Total cost) fv float value in a string
  51.     float amt;          // Input for Principal
  52.     float rate;         // Input for Interest Rate    
  53.     float ic;           // Interest Payed
  54.     float pmt;          // Payment
  55.     float fv;           // Total Cost
  56.     int term;         // # of months for the term
  57.     float r, temp;
  58.     float exp, prin, x, y, yrint = 0;
  59.     int mnbr;
  60.     int month, i, k, yr=1;
  61.         int dummy;          // Used by the AlertPanel
  62.     
  63.            
  64.      // Gets the value from the Principle field
  65.          amt = [Principal floatValue];
  66.  
  67. // If the value of the loan is over 49000$, it will
  68. // display this alert panel    
  69.     if (amt > 49000) {
  70. dummy = NXRunAlertPanel("Are you serious?","Do you really want to spend that much money?","Think about it!",NULL, NULL);
  71.                                    }
  72.  
  73. // Gets the value from the Years and Months field in the Term box
  74. // Then multiplies Years by 12 and adds the value of Months to the term int
  75.      term = ([Years floatValue]*12)+[Months floatValue];
  76.      
  77.      // Gets the value from the Interest rate % field
  78.      rate = [InterestRate floatValue];
  79.  
  80.  
  81. //  Computes payment and future value
  82.  
  83.     r = rate/100.0;
  84.     x = 1.0 + r/12.0;
  85.     y = term;
  86.     temp = (1.0 / pow(x,y));
  87.     pmt = (amt*r/12.0) / (1-temp);
  88.     k = term;
  89.     fv = pmt*k;
  90.     ic = fv - amt;
  91.  
  92.     // Clears the AmortDisplay scrollView
  93.     [[AmortDisplay docView] setText:""];
  94.          
  95.         // Places the values in thier proper field on the screen
  96.     
  97.     sprintf( pmtString, "$%.2f", pmt); // Convert float to a string
  98.     [Payment setStringValue:pmtString];// Result for Payment
  99.     
  100.     sprintf( icString, "$%.2f", ic); // Convert float to a string
  101.     [InterestPayed setStringValue:icString];// Result for Interest Payed
  102.     
  103.     sprintf( fvString, "$%.2f", fv); // Convert float to a string
  104.     [TotalCost setStringValue:fvString]; // Result for Total Cost
  105.  
  106.         // Header in the AmortDisplay scrollView
  107.     sprintf(buffer,"\n     *** Amortize ***\n");
  108.     [self appendText:sender];
  109.         sprintf(buffer," The Amortization tool.\n\n");
  110.         [self appendText:sender];
  111.     sprintf(buffer,"Principal:  $%.2f\n", amt);
  112.     [self appendText:sender];
  113.     sprintf(buffer,"Interest rate:  %3.2f%%\n", rate);
  114.     [self appendText:sender];
  115.     sprintf(buffer,"Term of loan in months:  %d\n", term);
  116.     [self appendText:sender];
  117.     sprintf(buffer,"Monthly payment:  $%.2f\n", pmt);
  118.     [self appendText:sender];
  119.      sprintf(buffer,"Total interest charged:  $%.2f\n", ic);
  120.      [self appendText:sender];
  121.      sprintf(buffer,"Total cost of loan:  $%.2f\n", fv);
  122.      [self appendText:sender];
  123.      sprintf(buffer,"\nMonth\tInterest\t\tPrincipal\t\tBalance\n");     [self appendText:sender];
  124.  
  125.     
  126. // Start of loop to print amortization schedule
  127.        
  128.           mnbr=mbeg;
  129.           for (i=1; i<=k; i++) {
  130.           month = i;
  131.           exp = amt*(r/12.0);
  132.           yrint=yrint+exp;
  133.           prin = pmt-exp;
  134.           amt = amt-prin;
  135.           mnbr++;
  136. sprintf(buffer,"%d\t %.2f\t\t %.2f\t\t %.2f\n",mnbr-1, exp, prin, amt);    
  137.           [self appendText:sender];
  138.           if (mnbr > 12 ) { 
  139.                                    sprintf(buffer,"\t\tInterest paid for year %d is %.2f\n\n",yr,yrint); 
  140.                         [self appendText:sender];
  141.                      yr++;
  142.                                     yrint=0;
  143.                                     mnbr=1;
  144.                                       }
  145.                }
  146.         
  147.           if (mnbr !=1 && mnbr != 13) {
  148.                       sprintf(buffer,"\t\tInterest paid for year %d is %.2f\n\n",yr,yrint);
  149.                                   [self appendText:sender];
  150.                                  }
  151.  
  152.      return self; //normal return, end of Calc method
  153. }
  154.  
  155. // JANUARY
  156. - January:sender
  157. // Assigns a value to mbeg for the Starting month
  158. {
  159.     mbeg = 1;
  160.     return self;
  161. }
  162.  
  163. // FEBRUARY
  164. - February:sender
  165. {
  166.     mbeg = 2;
  167.     return self;
  168. }
  169.  
  170. // MARCH
  171. - March:sender
  172. {
  173.     mbeg = 3;
  174.     return self;
  175. }
  176.  
  177. // APRIL
  178. - April:sender
  179. {
  180.     mbeg = 4;
  181.     return self;
  182. }
  183.  
  184. // MAY
  185. - May:sender
  186. {
  187.     mbeg = 5;
  188.     return self;
  189. }
  190.  
  191. // JUNE
  192. - June:sender
  193. {
  194.     mbeg = 6;
  195.     return self;
  196. }
  197.  
  198. // JULY
  199. - July:sender
  200. {
  201.     mbeg = 7;
  202.     return self;
  203. }
  204.  
  205. // AUGUST
  206. - August:sender
  207. {
  208.     mbeg = 8;
  209.     return self;
  210. }
  211.  
  212. // SEPTEMBER
  213. - September:sender
  214. {
  215.     mbeg = 9;
  216.     return self;
  217. }
  218.  
  219. // OCTOBER
  220. - October:sender
  221. {
  222.     mbeg = 10;
  223.     return self;
  224. }
  225.  
  226. // NOVEMBER
  227. - November:sender
  228. {
  229.     mbeg = 11;
  230.     return self;
  231. }
  232.  
  233. // DECEMBER
  234. - December:sender
  235. {
  236.     mbeg = 12;
  237.     return self;
  238. }
  239.  
  240. // The two methods below getName and SaveFile are used by
  241. // the save to a file option in Amortize. These methods are used only
  242. // for the file to disk option
  243.  
  244. // SETFILENAME
  245. - setFileName:(const char *)aFilename
  246. {
  247.        if (filename) free(filename);
  248.     filename = malloc(strlen(aFilename)+1);
  249.      strcpy(filename, aFilename);
  250.     [AmortWindow setTitleAsFilename:aFilename];
  251.     return self;
  252. }
  253.  
  254. //SAVEAS
  255. - SaveAs:sender
  256. {
  257.     id panel;
  258.     const char *dir;
  259.     char *file;
  260.  
  261. // Prompt user for file name and save to that file
  262.  
  263.     if (filename==0) {
  264.     /* no filename; set up defaults */
  265.     dir = NXHomeDirectory();
  266.     file = (char *) [AmortWindow title];
  267.                          }
  268. else {
  269.     file = rindex(filename, '/');
  270.     if (file) {
  271.       dir = filename;
  272.       *file =0;
  273.       file++;
  274.                   }
  275. else {
  276.     dir = filename;
  277.     file = (char *) [AmortWindow title];
  278.      }
  279.      }
  280.  
  281.     panel = [SavePanel new];
  282.  
  283.     [panel setRequiredFileType: "amortize"];
  284.     if ([panel runModalForDirectory: dir
  285.                    file: file])  {
  286.            [self setFileName: [panel filename] ];
  287.  
  288.            return [self SaveFile: sender];
  289.                                  }
  290.     return nil; // didn't save
  291. }    
  292.  
  293.  
  294. // SAVEFILE
  295. - SaveFile:sender
  296. // This method generates an amortization table and prints the result in the
  297. // various fields on the screen and prints a file to the disk.
  298. // This method method really does the same thing as the Calc method
  299. // except it generates it's output to a file on the disk.
  300. {
  301.  
  302.     int fd;
  303.     NXStream *theStream;
  304.     
  305.     if (filename==0) return [self SaveAs: sender];
  306.      [AmortWindow setTitle: "Saving..."];
  307.  
  308.     fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  309.         if (fd<0) {
  310.           NXRunAlertPanel(0, "Cannot save file: %s",0,0,0,strerror(errno));
  311.           return self;
  312.                   }    
  313.  
  314.     theStream = NXOpenFile(fd, NX_WRITEONLY);
  315.     [[AmortDisplay docView] writeRichText:theStream];
  316.     
  317.     NXClose(theStream);
  318.     close(fd);
  319.  
  320.     [AmortWindow setTitleAsFilename: filename];
  321.     return self;
  322. }
  323.  
  324. // APPENDTEXT
  325. - appendText:sender
  326. // This method appends a string called buffer to the end 
  327. // of AmortDisplay scrollView
  328. {
  329.         int HowLongIsTheText;
  330.     
  331.     document = [AmortDisplay docView];
  332.         
  333.     // Determines how many characters are in the 
  334.         // AmortDisplay scrollView document
  335.     HowLongIsTheText = [document textLength]; 
  336.         
  337.         // Selects and empty selection, which in fact places
  338.         // the cursor at the end of the document.
  339.     [document setSel:HowLongIsTheText:HowLongIsTheText];
  340.         
  341.     // Replaces the selection (in this case it is empty) 
  342.         // with buffer text string.
  343.     [document replaceSel:buffer]; 
  344.  
  345.     return self; //normal return
  346. }
  347.  
  348. // PRINTTABLE
  349. - PrintTable:sender
  350. // This method will print the AmortDisplay to the printer
  351. // Printing is rather simple; just send printPSCode: to the text view
  352. // you wish to print. The print panel will automatically pop up and unless
  353. // the user cancels the printout the text view will be printed.
  354. {    
  355.     [[AmortDisplay docView] printPSCode:self];
  356.     return self;
  357.  }
  358.   
  359.  
  360. @end
  361.